clear

/* Use existing file "./output/intermediate/cities_patents_list" from make_cusp_cities.do */

// =================
// Patent Classes 
// =================

import 	delimited ".\input\CUSP\patents_cpc_categories_from_1900.csv"
duplicates drop patnum,  force

tempfile cpc
save	"`cpc'", replace
clear

*import 	delimited ".\input\CUSP\patents_ipc_categories_from_1900.csv"
*duplicates drop patnum,  force
*
*tempfile ipc
*save	"`ipc'", replace
*clear

// =========================================================
// Combine Dates/Inventors and Collapse by Month-Year-County
// =========================================================

use 	"./output/intermediate/cities_patents_list"
  
merge	m:1 patnum using "`cpc'"
/* _merge=1: international patents
   _merge=2: outside date range */
drop if _merge==2
drop 	_merge     

*merge	m:1 patnum using "`ipc'"
/* _merge=1: international patents
   _merge=2: outside date range */
*drop if _merge==2
*drop 	_merge   
 
*****************************  
** DEFINE RELEVANT CLASSES **  
gen 	class_1d = upper(substr(cpc_class,1,1))
gen		class_3d = upper(substr(cpc_class,1,3)) 

replace	class_1d = "0" if class_1d==""
codebook class_?d
***************************** 

drop 	pat_single_inv pat_has_assignee
 
preserve 
	collapse (sum) pat_*, by(f_m f_yr npi_id)
	
	tempfile totalpats
	save "`totalpats'", replace
restore

collapse (sum) pat_*, by(f_m f_yr npi_id class_1d)
rename 	pat_* pat_*_

reshape wide pat_*, i(f_m f_yr npi_id) j(class_1d) string

merge 1:1 f_m f_yr npi_id using "`totalpats'"
drop 	_merge
compress

sort 	f_yr f_m npi_id

gen 	f_myr = ym(f_yr, f_m)
format 	f_myr %tm

order 	npi_id f_myr f_yr f_m , first

** Extend and fill in 0s

xtset 	npi_id f_myr	
tsfill, full

drop 	f_yr f_m

unab pvars : pat_*

foreach v of local pvars {
	replace `v' = 0 if mi(`v')
}

order 	npi_id f_myr, first

keep if tin(1900m1, 1929m12)

compress 
save 	"./output/intermediate/cities_patents_wclass_19001929", replace

collapse (sum) pat_*, by(f_myr) 
drop 	*_all_*

save 	"./output/intermediate/agg_patents_wclass_19001929", replace

clear
